package util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;


/**
 * 
 * @author Jakobus Böttcher
 * @version 1.0.0
 */
public class Config {
	public static boolean DEBUG = new Boolean(getStaticProperty("sys_debug"));
	public static boolean SHOW_DB_CONFIG_DIALOG = new Boolean(getStaticProperty("db_config_dialog"));
	
	static Properties properties = new Properties();
	private String propertiesFileName = null;
	
	public static String APP_DIR = System.getProperties().getProperty("user.dir") + File.separator;
	public static String LINE_SEP = System.getProperty("line.separator");
	
	/**
	 * 
	 * @param filename
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public Config(String filename) throws FileNotFoundException, IOException{
		setPropertiesFileName(filename);
		properties.load(new BufferedReader(new FileReader(this.getPropertiesFileName())));
	}
	
	/**
	 * 
	 * @param key
	 * @return property by key
	 */
	public String getProperty(String key){
		return properties.getProperty(key);
	}
	
	/**
	 * 
	 * @param filename
	 */
	public void setPropertiesFileName(String filename){
		this.propertiesFileName = System.getProperty("user.dir")+File.separator+filename;
	}
	
	/**
	 * 
	 * @return filename of the properties file
	 */
	public String getPropertiesFileName(){
		return this.propertiesFileName;
	}
		
	/**
	 * 
	 * @throws IOException
	 */
	public void saveProperties() throws IOException{
		this.properties.store(new FileOutputStream(propertiesFileName),"");
	}

	

	public static String getStaticProperty(String key){
		properties = new Properties();
		try {
			properties.load(new BufferedReader(new FileReader(System.getProperty("user.dir")+File.separator+"config.ini")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties.getProperty(key);
	}
		
}
